package cn.com.yto56.cainiao.exponent.elasticsearch.service.impl;

import cn.com.yto56.basic.framework.plugin.elasticsearch.ElasticSearchStarter;
import cn.com.yto56.cainiao.exponent.common.ExponentType;
import cn.com.yto56.cainiao.exponent.common.ExponentTypeMapper;
import cn.com.yto56.cainiao.exponent.common.QueryDSL;
import cn.com.yto56.cainiao.exponent.dto.ExponentResponse;
import cn.com.yto56.cainiao.exponent.elasticsearch.service.CommonService;
import cn.com.yto56.cainiao.exponent.elasticsearch.service.QueryE2Service;
import cn.com.yto56.cainiao.exponent.elasticsearch.service.engine.Engine4ES;
import lombok.extern.slf4j.Slf4j;
import org.frameworkset.elasticsearch.entity.MapRestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class QueryE2ServiceImpl implements QueryE2Service {

    @Autowired
    private ElasticSearchStarter starter;

    @Override
    public ExponentResponse queryLineAging(Date pullDate) {
        Map<String, String> map = CommonService.getSearchTime(pullDate, 1);
        map.put("exponentType", ExponentType.LINE_AGING.getName());
        MapRestResponse res = query(pullDate, QueryDSL.AGG, map);
        return Engine4ES.read(ExponentType.LINE_AGING, res);
    }

    @Override
    public ExponentResponse queryTimelyCanvass(Date pullDate) {
        Map<String, String> map = CommonService.getSearchTime(pullDate, 1);
        MapRestResponse lanShou = query(pullDate, QueryDSL.TIMELY_CANVASS_LAN_SHOU, map);
        ExponentResponse responseLanShou = Engine4ES.read(ExponentType.PERFECTION, lanShou);

        map.put("exponentType", ExponentType.TIMELY_CANVASS.getName());
        MapRestResponse res = query(pullDate, QueryDSL.AGG, map);
        ExponentResponse response = Engine4ES.read(ExponentType.TIMELY_CANVASS, res);

        response.getData().put(ExponentTypeMapper.get("超18小时"), responseLanShou.getTotal());
        return response;
    }

    @Override
    public ExponentResponse queryPerfection(Date pullDate) {
        Map<String, String> map = CommonService.getSearchTime(pullDate, 1);
        MapRestResponse guoJi = query(pullDate, QueryDSL.PERFECTION_GUO_JI, map);
        ExponentResponse responseGuoJi = Engine4ES.read(ExponentType.PERFECTION, guoJi);

        map.put("exponentType", ExponentType.PERFECTION.getName());
        MapRestResponse res = query(pullDate, QueryDSL.AGG, map);
        ExponentResponse response = Engine4ES.read(ExponentType.PERFECTION, res);

        response.getData().put(ExponentTypeMapper.get("国际件"), responseGuoJi.getTotal());
        return response;
    }

    @Override
    public ExponentResponse queryAccuracy(Date pullDate) {
        Map<String, String> map = CommonService.getSearchTime(pullDate, 1);
        MapRestResponse agentPoint = query(pullDate, QueryDSL.ACCURACY_AGENT_POINT, map);
        ExponentResponse responseAgentPoint = Engine4ES.read(ExponentType.ACCURACY, agentPoint);

        map.put("exponentType", ExponentType.ACCURACY.getName());
        MapRestResponse res = query(pullDate, QueryDSL.AGG, map);
        ExponentResponse response = Engine4ES.read(ExponentType.ACCURACY, res);

        response.getData().putAll(responseAgentPoint.getData());
        return response;
    }


    @Override
    public List<String> getDays(int maxDays, String fmt) {
        List<String> res = new ArrayList<>();
        if (maxDays > 0) {
            SimpleDateFormat dateFormat = new SimpleDateFormat(fmt);
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            res.add(dateFormat.format(c.getTime()));
            for (int i = 1; i <= maxDays; i++) {
                c.add(Calendar.DAY_OF_MONTH, -1);
                res.add(dateFormat.format(c.getTime()));
            }
        }
        return res;
    }

    private MapRestResponse query(final Date pullDate, final String s, final Map<String, String> map) {
        String path = CommonService.getTrackESPath(pullDate);
        String e = CommonService.compileString(s, map);
        return CommonService.search(starter.getRestClient(), path, e);
    }
}
